#include "tcSocketCommonFunctions.h"

#pragma warning(disable : 4996)
#pragma warning(disable : 4996)

int tc_socket_cf_process_msg(int recv_len, char *msg, char *tag, char *content)
{
	char ch;
	int i;

	memset(tag, 0, recv_len);
	memset(content, 0, recv_len);

	printf("ProcessMsg: receive %d bytes %s\n", recv_len, msg);
	tag[0] = content[0] = '\0';
	for (i=0; i<recv_len-3; ++i) {
		ch = msg[i];
		if (msg[i] == '|' && msg[i+1] == '|' && msg[i+2] == '|') {
			memcpy(tag, msg, i);
			tag[i] = '\0';
			memcpy(content, &msg[i+3], recv_len-i-3);
			content[recv_len-i-3] = '\0';
			printf("ProcessMsg: tag %s content %s\n", tag, content);
			return atoi(tag);
		}
	}
	printf("ProcessMsg: no SPLIT signal.\n");
	return -1;
}

int tc_socket_cf_login(ClientOpt * opt)
{
	int len;
	int error_code;

	if (opt->pending == true) {
		printf("Socket is busy now.\n");
		return -1;
	}
	itoa(ASK_LOGIN, opt->buffer, 10);
	strcat(opt->buffer, SPLIT);
	strcat(opt->buffer, opt->user_name);
	strcat(opt->buffer, ":");
	strcat(opt->buffer, &opt->local_port[0]);

	len = strlen(opt->buffer);
	opt->data_len = len+1;
	opt->pending = true;

	error_code = WSAGetLastError();

	return error_code;
}

int tc_socket_cf_exit_game(ClientOpt * opt)
{
	if (opt->pending == true) {
		printf("Socket is busy now.\n");
		return -1;
	}
	itoa(ASK_EXIT_GAME, opt->buffer, 10);
	strcat(opt->buffer, "|||");
	strcat(opt->buffer, opt->user_name);

	opt->data_len = strlen(opt->buffer)+1;
	opt->pending = true;

	return 0;
}

int tc_socket_cf_get_user_list(ClientOpt * opt)
{
	if (opt->pending == true) {
		printf("Socket is busy now.\n");
		return -1;
	}
	itoa(ASK_ALL_USER, opt->buffer, 10);
	strcat(opt->buffer, SPLIT);
	strcat(opt->buffer, opt->user_name);
	opt->data_len = strlen(opt->buffer)+1;
	opt->pending = true;

	return 0;
}

int tc_socket_cf_connect_user(ClientOpt * opt, char * guest)
{
	if (opt->pending) {
		printf("Socket is busy now.\n");
		return -1;
	}
	itoa(ASK_INVITE, opt->buffer, 10);
	strcat(opt->buffer, SPLIT);
	strcat(opt->buffer, opt->user_name);
	strcat(opt->buffer, ":");
	strcat(opt->buffer, guest);
	opt->data_len = strlen(opt->buffer)+1;

	opt->pending = true;

	return 0;
}

int tc_socket_cf_send_data(ClientOpt * opt, void * data, int length)
{
	int len;

	if (opt->pending) {
		printf("Socket is busy now.\n");
		return -1;
	}
	itoa(ASK_DATA, opt->buffer, 10);
	strcat(opt->buffer, SPLIT);
	strcat(opt->buffer, opt->user_name);
	strcat(opt->buffer, ":");

	len = strlen(opt->buffer);
	memcpy(&opt->buffer[len], data, length);
	opt->data_len = len + length;
	opt->pending = true;

	return 0;
}

int tc_socket_cf_send_info(char * server_name, int port, void * content, int len)
{
	ClientOpt opt;
	HANDLE h;
	int i;

	printf("SendInfo : %s %d %s %d\n", server_name, port, content, len);
	opt.buffer_len = 1024;
	opt.buffer = (char*)malloc(sizeof(char)*opt.buffer_len);

	for (i=0; i<len; ++i)
		printf("%d", ((char*)content)[i]);
	printf("\n");

	memcpy(opt.buffer, content, len);
	opt.data_len = len;
	opt.pending = true;
	opt.runonce = true;
	opt.remote_port = port;
	strcpy(opt.server_name, server_name);

	h = (HANDLE)_beginthread(tc_socket_create_client, 0, &opt);
	WaitForSingleObject(h, INFINITE);

	return 0;
}
